/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * IdentifiedSessionFactory.java
 * common-config
 * 2013-5-10上午11:24:25
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.config;

import java.io.IOException;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.stereotype.Component;


@Component("idtSessionFactory")
public class IdentifiedSessionFactory extends  AnnotationSessionFactoryBean{
private static final Log log = LogFactory.getLog(IdentifiedSessionFactory.class);
	
	private ResourcePatternResolver resourceResolver;
		

	@Resource(name="mappedXml")
	public void setMappedXmlResource(ResourceSet mappedXmlResource) {
		String[] mappingResources = mappedXmlResource.toArray(new String[0]);
		this.setMappingResources(mappingResources);		
	}
	
	@Resource(name="scannedPackage")
	public void setPackage4Scan(ResourceSet package4Scan) {
		String[] packagesToScan = package4Scan.toArray(new String[0]);
		log.info(package4Scan);
		this.setPackagesToScan(packagesToScan);
	}
		
	
	
	@Override
	@Resource(name="idtds")
	public void setDataSource(DataSource dataSource) {
		super.setDataSource(dataSource);
	}

	@Override
	public void setHibernateProperties(Properties hibernateProperties) {
		org.springframework.core.io.Resource res = resourceResolver.getResource("classpath*:hibernate.properties");
		Properties  hp = new Properties();
		try {
			hp.load(res.getInputStream());
			super.setHibernateProperties(hp);
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}	
	}

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		super.setResourceLoader(resourceLoader);
		resourceResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
	  
	}
	
	@PostConstruct
	public void init(){
	   
	   
	}
}
